package io.renren.api;


import io.renren.core.annotation.LoginUser;
import io.renren.entity.Book;
import io.renren.entity.Dictionary;
import io.renren.entity.WxUser;
import io.renren.entity.vo.GradeSubject4Web;
import io.renren.entity.vo.VideoTapRecord;
import io.renren.service.BookService;
import io.renren.service.DictionaryService;
import io.renren.service.GradeSubjectService;
import io.renren.utils.ConfigConstant;
import io.renren.utils.JsonUtils;
import io.renren.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

import static java.util.Comparator.comparingLong;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;

@Slf4j
@RestController
@RequestMapping("/api/home")
public class ApiHomeController {

    @Autowired
    private GradeSubjectService gradeSubjectService;

    @Autowired
    private BookService bookService;

    @Autowired
    private DictionaryService dictionaryService;

    /**
     * 查询所有的年级科目列表
     *
     * @return
     */
    @RequestMapping("/getGradeAndSubject")
    public R getGradeAndSubject() {
        //查询所有可以展示的年级科目
        List<GradeSubject4Web> subject4WebList = gradeSubjectService.queryList4web();
        Map<Long, List<GradeSubject4Web>> collect = subject4WebList.stream()
                .sorted(Comparator.comparing(GradeSubject4Web::getGradeOrder))
                .collect(Collectors.groupingBy(GradeSubject4Web::getGradeId));

        return R.ok().put("gradeSubjectMap", collect);
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    @RequestMapping("/getUserInfo")
    public R getUserInfo(@LoginUser WxUser wxUser) {
        String gradeId = wxUser.getGradeId() == null ? ConfigConstant.DEFAULT_GRADE_ID : wxUser.getGradeId() + "";
        String identity = wxUser.getIdentity();

        return R.ok()
                .put("userId",wxUser.getId())
                .put("gradeId", gradeId)
                .put("identity", identity);
    }

    /**
     * 根据年级科目获取教版信息【需要过滤掉教辅书中不存在的教版】
     *
     * @return
     */
    @RequestMapping("/getVersionList")
    public R getVersionList( @RequestParam String gradeId, @RequestParam String subjectId) {

        Map query = new HashMap();
        query.put("type", "T03");
        query.put("status", "1");
        query.put("deleted", "0");
        List<Dictionary> versionList = dictionaryService.queryList(query);

        //根据年级科目获取视频列表
        Map bookQuery = new HashMap();
        bookQuery.put("status", "1");
        bookQuery.put("deleted", "0");
        bookQuery.put("gradeId", gradeId);
        bookQuery.put("subjectId", subjectId);
        bookQuery.put("seasonId", ConfigConstant.DEFAULT_SEASON_ID);
        List<Book> bookList = bookService.queryList(bookQuery);

        ArrayList<Dictionary> collect = bookList.stream()
                .map(book -> {
                    Dictionary version = new Dictionary();
                    for (Dictionary dictionary : versionList) {
                        if (dictionary.getId().equals(book.getVersionId())) {
                            version = dictionary;
                            break;
                        }
                    }
                    return version;
                })
                .sorted(Comparator.comparingInt(Dictionary::getOrderField))
                .collect(collectingAndThen(toCollection(() -> new TreeSet<>(comparingLong(Dictionary::getId))), ArrayList::new));

        return R.ok().put("versionList", collect);
    }

    @RequestMapping("/setUserVideoInfo")
    public R setUserVideoInfo(@RequestParam Integer status, @RequestParam Long videoId, @LoginUser WxUser wxUser) {
        VideoTapRecord record = new VideoTapRecord();
        record.setStatus(status);
        record.setVideoId(videoId);
        record.setOpenId(wxUser.getOpenId());
        log.info(JsonUtils.toJson(record));
        return R.ok();
    }

}
